package com.caregiver.watch.business.care.enums;

import com.caregiver.watch.business.care.model.po.UserPackageOrder;
import com.caregiver.watch.common.utils.number.IntegerUtils;
import lombok.Getter;

import java.time.LocalDateTime;

import static com.caregiver.watch.common.constant.NumberConstants.MINUS_ONE;

/**
 * @Description: 套餐类型
 * @Author 疆戟
 * @Date 2025/10/13 21:51
 * @Version 1.0
 */
@Getter
public enum PackageTypeEnum {

    /**
     * 1.次数套餐
     * 2.时长套餐
     * 3.连续时长套餐
     */
    COUNT(1, "C","次数套餐") {

        @Override
        protected Boolean isEffective(UserPackageOrder order) {
            return LocalDateTime.now().isAfter(order.getEffectiveTime())
                    && IntegerUtils.ofNullable(order.getPackageValue()) > IntegerUtils.ofNullable(order.getWarningCount());
        }

        @Override
        protected Boolean isExpired(UserPackageOrder order) {
            return IntegerUtils.ofNullable(order.getWarningCount()) >= IntegerUtils.ofNullable(order.getPackageValue());
        }
    },

    DURATION(2, "D", "时长套餐") ,

    CONTINUOUS_DURATION(3, "CD", "连续时长套餐") ;

    private final Integer code;

    private final String prefix;

    private final String desc;

    PackageTypeEnum(Integer code, String prefix, String desc) {
        this.code = code;
        this.prefix = prefix;
        this.desc = desc;
    }



    /**
     * 根据code获取枚举
     * @param code
     * @return
     */
    public static PackageTypeEnum pares(Integer code) {
        for (PackageTypeEnum value : values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 判断套餐是否生效
     */
    protected Boolean isEffective(UserPackageOrder order) {
        LocalDateTime now = LocalDateTime.now();
        return now.isAfter(order.getEffectiveTime())
                && now.isBefore(order.getExpireTime());
    }

    /**
     * 判断套餐是否已失效
     */
    protected Boolean isExpired(UserPackageOrder order) {
        return LocalDateTime.now().isAfter(order.getExpireTime());
    }

    /**
     * 获取套餐生效状态
     */
    public PackageEffectivenessStatusEnum getEffectiveStatus(UserPackageOrder order) {
        if (isExpired(order)) {
            return PackageEffectivenessStatusEnum.EXPIRED;
        } else if (isEffective(order)) {
            return PackageEffectivenessStatusEnum.EFFECTIVE;
        }
        return PackageEffectivenessStatusEnum.PENDING;
    }
}
